package org.cs.web;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UserDetailUpdate
 */
public class UserDetailUpdate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserDetailUpdate() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String un = (String)request.getSession().getAttribute("userName");
		if(un==null ||"".equals(un)){
			request.getRequestDispatcher("cs_login/login.jsp").forward(request, response);
			return;
		}
		
		String id = request.getParameter("id");
		String nickName = request.getParameter("nickName");
		String email =request.getParameter("email");
		String passwd = request.getParameter("passwd");
		String intro = request.getParameter("intro");
		String sex = request.getParameter("sex");
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/cs?useUnicode=true&characterEncoding=utf-8","root","123123");
			String sql="UPDATE user_detail set nick_name=?,email=?,passwd=?,intro=?,sex=? where id=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, nickName);
			pstmt.setString(2, email);
			pstmt.setString(3, passwd);
			pstmt.setString(4, intro);
			pstmt.setInt(5, Integer.parseInt(sex));
			pstmt.setInt(6, Integer.parseInt(id));
			pstmt.executeUpdate();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		request.getRequestDispatcher("UserDetailQuery").forward(request, response);
	}

}
